package me.xiaoyan.java.chapter13;

import java.util.Scanner;

public class Game {
	/**
	 * 电脑玩家
	 */
	Computer pcPlayer;
	/**
	 * 人
	 */
	Person   personPlayer;
	/**
	 * 控制台输入对象
	 */
	Scanner  input = new Scanner(System.in);
	/**
	 * 游戏次数
	 */
	int playCount = 0;
	
	/**
	 * 初始化游戏信息
	 */
	public void initGame(){
		System.out.println("*********欢迎进入游戏世界*********");
		System.out.println();
		System.out.println("\t\t\t***********");
		System.out.println("\t\t\t* 游戏开始 *");
		System.out.println("\t\t\t***********");
		System.out.println("出拳规则: 1.剪刀 2.石头 3.布");
		//初始化人数数据
		initPlayerData();
		
		
	}
	
	/**
	 * 初始化玩家数据
	 */
	public void initPlayerData(){
		System.out.println("请选择对方:1.刘备 2.孙权 3.曹操");
		
		//创建电脑角色，并选择对手名字
		int choice = input.nextInt();
		pcPlayer = new Computer();
		switch(choice)
		{
		case 1:
			pcPlayer.name = "刘备";
			break;
		case 2:
			pcPlayer.name = "孙权";
			break;
		default:
			pcPlayer.name = "曹操";
			break;
		}
		System.out.println("您选择了" + pcPlayer.name + "作为对手!");
		
		//创建任务角色并取名字
		personPlayer = new Person();
		System.out.println("请输入角色名字:");
		personPlayer.name = input.next();
		
	}
	
	/**
	 * 开始游戏
	 */
	public void startGame(){
		while(true){
			System.out.print("是否开始(y/n)?");
			String answer = input.next();
			if("y".equals(answer)){//如果回答y就开始游戏
				playGame();
			}else{
				break;
			}
		}
		
		System.out.println("退出游戏");
		showGameResult();//显示游戏结果
	}
	
	/**
	 * 显示游戏战果
	 */
	public void showGameResult(){
		//比较分数
		String scoreResult = "";
		if(pcPlayer.score > personPlayer.score){
			scoreResult = "呵呵呵呵，笨蛋，下次继续努力";
		}else if(pcPlayer.score < personPlayer.score){
			scoreResult = "哎哟，能够打败我，还不错哦";
		}else{
			scoreResult = "小子实力还不错嘛，居然能够和我匹敌";
		}
		
		
		System.out.println("-------------------------");
		System.out.println(pcPlayer.name + " VS " + personPlayer.name);
		System.out.println(pcPlayer.score + " : " + personPlayer.score);
		System.out.println("对战次数:" + playCount);
		System.out.println("对战结果:" + scoreResult);
		System.out.println("-------------------------");
	}
	
	/**
	 * 玩游戏：主要定义了玩一局游戏的流程
	 * 1.剪刀 2.石头 3.布
	 */
	public void playGame(){
		playCount ++;
		//人出一拳
		int personFist = personPlayer.showFist();
		//电脑出一拳
		int pcFist = pcPlayer.showFist();
		
		if(pcFist == personFist){//平局
			System.out.println("唉，运气真不好，居然是平局");
		}else if(
		 (personFist == 1 && pcFist == 3)//剪刀 - 布
	  || (personFist == 2 && pcFist == 1)//石头 - 剪刀
	  || (personFist == 3 && pcFist == 2)//布   - 石头
				){//人赢
			System.out.println("哦哦哦，真幸运，居然赢了");
			personPlayer.score ++;
		}else{//电脑赢
			System.out.println("你真笨，这么简单都输了");
			pcPlayer.score ++;
		}
		
	}
	
	public static void main(String[] args) {
		Game gameObject = new Game();
		gameObject.initGame();//初始化游戏
		gameObject.startGame(); //开始游戏
	}
}
